package com.sxx.beautifulcity.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.gitonway.lee.niftymodaldialogeffects.lib.Effectstype;
import com.gitonway.lee.niftymodaldialogeffects.lib.NiftyDialogBuilder;
import com.sxx.beautifulcity.R;
import com.sxx.beautifulcity.database.UserHelper;
import com.sxx.beautifulcity.entity.greendao.UserDaoEntity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/11/3.
 */
public class RegisterActivity extends AppCompatActivity implements View.OnClickListener {
    private Button registerBtn , kuaisu_register;
    private EditText registerNumberEt , zhanghaoName;
    private EditText pawRegister;
    private UserHelper helper;
    private String registerName;
    private String registerPsw;
    private TextView register;
    private Effectstype effect;
    private String zhanghao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        register = (TextView) findViewById(R.id.register);
//        register.setSelected(true);
        register.requestFocus();
        registerBtn = (Button) findViewById(R.id.registerBtn);
        registerNumberEt = (EditText) findViewById(R.id.registerNumberEt);
        pawRegister = (EditText) findViewById(R.id.pawRegister);
        registerBtn.setOnClickListener(this);
        helper = new UserHelper();
        kuaisu_register = (Button) findViewById(R.id.kuaisu_register);
        kuaisu_register.setOnClickListener(this);
        zhanghaoName = (EditText) findViewById(R.id.zhanghaoName);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.kuaisu_register:
                Intent intentRegister = new Intent();
                intentRegister.setClass(RegisterActivity.this , LoginActivity.class);
                startActivity(intentRegister);
                finish();
                overridePendingTransition(R.anim.left_out, R.anim.right_into);
                break;

            case R.id.registerBtn:
                registerName = registerNumberEt.getText().toString();
                registerPsw = pawRegister.getText().toString();
                zhanghao = zhanghaoName.getText().toString();

                if (registerName == null || registerName.length() != 11) {
                    showAlertDialog();
                } else {
                    List<UserDaoEntity> entities = helper.queryName(registerName , zhanghao);
                    if (entities != null && entities.size() > 0) {
                        showNamehas();
                        registerNumberEt.setText("");
                        pawRegister.setText("");
                        zhanghaoName.setText("");
                    } else {
                        UserDaoEntity userDaoEntity = new UserDaoEntity();
                        userDaoEntity.setUserName(registerName);
                        userDaoEntity.setUserPsw(registerPsw);
                        userDaoEntity.setZhanghao(zhanghao);
                        List<UserDaoEntity> list = new ArrayList<>();
                        list.add(userDaoEntity);
                        helper.savaRegisterList(list);
                        Intent intent = new Intent(this, LoginActivity.class);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(R.anim.left_out, R.anim.right_into);
                    }
                }
                break;
        }
    }
    /**
     * 注册不合法
     */

    private void showAlertDialog() {

        final NiftyDialogBuilder dialogBuilder = NiftyDialogBuilder.getInstance(this);
        dialogBuilder
                .withTitle(null)                                  //.withTitle(null)  no title
                .withTitleColor("#FFFFFF")                                  //def
                .withDividerColor("#f3aece")                              //def
                .withMessage("亲,请输入正确的用户名哦")                     //.withMessage(null)  no Msg
                .withMessageColor("#FFFFFFFF")                              //def  | withMessageColor(int resid)
                .withDialogColor("#f3aece")                               //def  | withDialogColor(int resid)                               //def
                .withIcon(getResources().getDrawable(R.mipmap.ic_launcher))
                .isCancelableOnTouchOutside(true)                           //def    | isCancelable(true)
                .withDuration(700)                                          //def
                .withEffect(effect)                                         //def Effectstype.Slidetop
                .withButton1Text("去注册")                                      //def gone
                .withButton2Text("取消")                                  //def gone//.setCustomView(View or ResId,context)
                .setButton1Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(RegisterActivity.this, RegisterActivity.class);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(R.anim.left_out, R.anim.right_into);
                    }
                })
                .setButton2Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogBuilder.dismiss();
                    }
                });
        dialogBuilder.show();
    }

    /**
     * 账号已经存在 请重新注册
     **/

    private void showNamehas() {

        final NiftyDialogBuilder dialogBuilder = NiftyDialogBuilder.getInstance(this);
        dialogBuilder
                .withTitle(null)                                  //.withTitle(null)  no title
                .withTitleColor("#FFFFFF")                                  //def
                .withDividerColor("#f3aece")                              //def
                .withMessage("亲,用户名已经存在,请重新注册哦")                     //.withMessage(null)  no Msg
                .withMessageColor("#FFFFFFFF")                              //def  | withMessageColor(int resid)
                .withDialogColor("#f3aece")                               //def  | withDialogColor(int resid)                               //def
                .withIcon(getResources().getDrawable(R.mipmap.ic_launcher))
                .isCancelableOnTouchOutside(true)                           //def    | isCancelable(true)
                .withDuration(700)                                          //def
                .withEffect(effect)                                         //def Effectstype.Slidetop
                .withButton1Text("去注册")                                      //def gone
                .withButton2Text("取消")                                  //def gone//.setCustomView(View or ResId,context)
                .setButton1Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setClass(RegisterActivity.this, RegisterActivity.class);
                        startActivity(intent);
                        finish();
                        overridePendingTransition(R.anim.left_out, R.anim.right_into);
                    }
                })
                .setButton2Click(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialogBuilder.dismiss();
                    }
                });
        dialogBuilder.show();

    }
}
